package com.szh.zx.duoxiancheng.chapter1;

import java.util.concurrent.atomic.AtomicInteger;

/**仅仅用于演示，实际开发中使用 ReentrantLock
 * 使用AtomicInteger实现锁
 * */
public class MyLock {
    private AtomicInteger status = new AtomicInteger(0);

    public void lock(){
        while (!status.compareAndSet(0,1)){
            Thread.yield();
        }
    }
    public void unlock(){
        // 0-未锁定
        status.compareAndSet(1,0);
    }
}
